package com.taptech.doufu.ui.view.choice.base;

import android.content.Context;
import androidx.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.taptech.doufu.R;
import com.taptech.doufu.bean.EventBean;
import com.taptech.doufu.bean.choice.ChoiceTopicBean;
import com.taptech.doufu.constant.MoreType;
import com.taptech.doufu.constant.SelectBlockTypes;
import com.taptech.doufu.constant.UmengEventName;
import com.taptech.doufu.ui.view.choice.ChoiceUtils;
import com.taptech.doufu.ui.view.countdownview.CustomCountDownTimer;
import com.taptech.doufu.ui.view.countdownview.DateUtils;
import com.taptech.doufu.util.TimeDateFunction;

public abstract class ChoiceTopicBaseView extends LinearLayout {
    private TextView tvMore,tvTitle,tvHour,tvMinute,tvSecond;
    protected View llTime,llMore;
    private CustomCountDownTimer timer;
    protected String replaceUrl;
    protected EventBean eventData;


    public ChoiceTopicBaseView(Context context) {
        super(context);
    }

    public ChoiceTopicBaseView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public ChoiceTopicBaseView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void initTop(View view, final ChoiceTopicBean bean) {
        eventData = bean.getEventData();
        replaceUrl = bean.getMoreUrl();
        tvMore = view.findViewById(R.id.tvMore);
        tvTitle = view.findViewById(R.id.tvTitle);
        llMore = view.findViewById(R.id.llMore);
        llTime = view.findViewById(R.id.llTime);
        tvMinute = view.findViewById(R.id.tvMinute);
        tvHour = view.findViewById(R.id.tvHour);
        tvSecond = view.findViewById(R.id.tvSecond);
        tvTitle.setText(bean.getTitle());
        if (bean.getMoreType() == MoreType.MORE) {
            llTime.setVisibility(View.GONE);
            llMore.setVisibility(View.VISIBLE);
            tvMore.setText("更多");
        }else if(bean.getMoreType()  == MoreType.REPLACE){
            llTime.setVisibility(View.GONE);
            llMore.setVisibility(View.VISIBLE);
            tvMore.setText("换一批");
        }else if (bean.getMoreType()  == MoreType.COUNTDOWN){
            llTime.setVisibility(View.VISIBLE);
            llMore.setVisibility(View.GONE);
            startTime(bean.getDeadline());
        }else{
            llTime.setVisibility(View.INVISIBLE);
            llMore.setVisibility(View.INVISIBLE);
        }
        llMore.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (bean.getMoreType()  == MoreType.MORE) {
                    ChoiceUtils.addEvent(getContext(),UmengEventName.JINGXUAN_CARD_MORE,bean.getEventData());
                    if (bean.getType() == SelectBlockTypes.COMICS) {
                        ChoiceUtils.jumpToMoreComic(getContext(), bean.getTitle(), bean.getMoreUrl());
                    }else{
                        ChoiceUtils.jumpToMoreNovel(getContext(),bean.getTitle(),bean.getMoreUrl());
                    }
                } else if (bean.getMoreType()  == MoreType.REPLACE) {
                    ChoiceUtils.addEvent(getContext(),UmengEventName.JINGXUAN_CARD_REPLACE,bean.getEventData());
                    replaceData(replaceUrl);
                }
            }
        });
    }




    public abstract void replaceData(String moreUrl);




    private void startTime(String deadline) {
        if (TextUtils.isEmpty(deadline)) {
            return;
        }
        long endTime = TimeDateFunction.getTimeToLong(deadline);
        long time = (long) ((double) endTime*1000- System.currentTimeMillis());
        timer = new CustomCountDownTimer(time,1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                tvHour.setText(DateUtils.INSTANCE.getHour(millisUntilFinished));
                tvMinute.setText(DateUtils.INSTANCE.getMinute(millisUntilFinished));
                tvSecond.setText(DateUtils.INSTANCE.getSecond(millisUntilFinished));
            }

            @Override
            public void onFinish() {
                //拉取新数据
            }
        };
        timer.start();

    }
}
